package org.apache.struts.ehr.patient_details.action;

import java.util.Date;
import java.util.List;
import org.apache.struts.ehr.medical_records.model.ReplyMessage;
import org.apache.struts.ehr.patient_details.model.*;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v251.message.ADR_A19;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.PipeParser;

import com.earogya.Constants;
import com.earogya.communicate.Communicator;
import com.earogya.logic.medical_records.ParseReply;
import com.earogya.logic.patient_details.ADR_A19_Handler;
import com.earogya.logic.patient_details.CreateQRYA19;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * Action class for search patients
 * 
 * @author Ureka
 * 
 */
public class SearchAction extends ActionSupport {

	private PatientDetails patientDetails;
	private HL7Message message;
	private Date date;
	private ReplyMessage replyMessage;
	private List<PatientDetails> patientDetailsList;
	private SelectLists selectLists;

	/**
	 * 
	 * Execute method for Search patients
	 * 
	 */
	public String execute() throws Exception {
		CreateQRYA19 createMessage = new CreateQRYA19(patientDetails);
		replyMessage = new ReplyMessage();
		replyMessage.setMessage(Constants.EMPTY);
		String msg = createMessage.createMessage(Constants.SENDING_FACILITY);
		message = new HL7Message();
		if (!msg.equals(null)) {
			Communicator c = new Communicator();
			String reply = c.sendMessage(msg);
			if (!reply.equals("No Reply")) {
				ParseReply parse = new ParseReply();
				try {
					// this.selectLists=new SelectLists();
					ADR_A19_Handler handler = new ADR_A19_Handler(
							createADRA19Msg(reply.toString()));
					patientDetailsList = handler.manageMessage();
				} catch (ClassCastException e) {
					replyMessage = (ReplyMessage) parse.parseMessage(reply);
					
				}
			}
		}

		return SUCCESS;
	}

	public ADR_A19 createADRA19Msg(String message) throws EncodingNotSupportedException, HL7Exception {
		Message hapiMsg = new PipeParser().parse(message);
		return (ADR_A19) hapiMsg;
	}

	/**
	 * 
	 * Validate user inputs
	 * 
	 */
	public void validate() {
		if ((patientDetails.getPatient().getPatientId() == null || patientDetails
				.getPatient().getPatientId().equals(""))
				&& (patientDetails.getPatient().getGivenName() == null || patientDetails
						.getPatient().getGivenName().equals(""))
				&& (patientDetails.getPatient().getFamilyName() == null || patientDetails
						.getPatient().getFamilyName().equals(""))
				&& (patientDetails.getPatient().getNicNo() == null || patientDetails
						.getPatient().getNicNo().equals(""))) {
			addFieldError("patientDetails.patient.nicNo",
					"Provide atleast one field for search");
		}
	
		else if(!(patientDetails.getPatient().getPatientId() == null || patientDetails
				.getPatient().getPatientId().equals(""))){
			if (!this.isInteger(patientDetails.getPatient().getPatientId())) {
				addFieldError("patientDetails.patient.patientId",
						"Incorrect patient ID format.");
		}
	}

		if (patientDetails.getPatient().getNicNo() == null
				|| patientDetails.getPatient().getNicNo().equals("")) {

		} else if (!this.isCorrectNICNo(patientDetails.getPatient().getNicNo())) {
			addFieldError("patientDetails.patient.nicNo",
					"Incorrect NIC format.");
		}
	}

	/**
	 * 
	 * Checks whether given string is an integer
	 * 
	 * @param value
	 * @return
	 */
	private Boolean isInteger(String value) {
		Boolean isInteger = false;
		try {
			int intValue = Integer.parseInt(value);
			isInteger = true;
		} catch (NumberFormatException e) {
			isInteger = false;
		}
		return isInteger;
	}

	/**
	 * 
	 * Check whether the given string is in correct NIC format
	 * 
	 * @param nic
	 * @return
	 */
	private Boolean isCorrectNICNo(String nic) {
		Boolean isCorrectNicNo = false;
		int length = nic.length();
		if (length == 10 && (nic.charAt(9) == 'v' || nic.charAt(9) == 'V')) {
			isCorrectNicNo = true;
		}
		return isCorrectNicNo;
	}

	public List<PatientDetails> getPatientDetailsList() {
		return patientDetailsList;
	}

	public SelectLists getSelectLists() {
		return selectLists;
	}

	public void setSelectLists(SelectLists selectLists) {
		this.selectLists = selectLists;
	}

	public void setPatientDetailsList(List<PatientDetails> patientDetailsList) {
		this.patientDetailsList = patientDetailsList;
	}

	public ReplyMessage getReplyMessage() {
		return replyMessage;
	}

	public void setReplyMessage(ReplyMessage replyMessage) {
		this.replyMessage = replyMessage;
	}

	public HL7Message getMessage() {
		return message;
	}

	public PatientDetails getPatientDetails() {
		return patientDetails;
	}

	public void setPatientDetails(PatientDetails patientDetails) {
		this.patientDetails = patientDetails;
	}

}
